package fy.hr.operator.impl;

import java.util.ArrayList;
import java.util.List;

import com.seeyon.v3x.services.flow.bean.FlowExport;

import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.operate.IFormOperator;
import fy.hr.bean.AwayChangeFromBean;
import fy.hr.constant.OA2HRConstant;
import fy.hr.dao.AwayTimeChangeFormDao;
import fy.util.MyUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AwayChangeApplicationFormOperator extends AForm_Save_Operator
		implements IFormOperator {
	private static Log log = LogFactory.getLog(AwayTimeChangeFormDao.class);
	private List<String> awaybegindayList = new ArrayList<String>();

	private List<String> awayenddayList = new ArrayList<String>();

	private AwayChangeFromBean bean = new AwayChangeFromBean();

	@Override
	protected void parseOtherField_FirstStep(FlowExport flowExport) {
		bean.setBillname(flowExport.getFlowTitle());
	}

	@Override
	protected void parseValueExport(
			com.seeyon.v3x.services.form.bean.ValueExport ve) {

		String fieldName = ve.getDisplayName();

		if ("出差地".equals(fieldName)) {
			bean.setAwayaddress(ve.getValue());
		} else if ("变更原因".equals(fieldName)) {
			bean.setNewAwaynote(ve.getValue());
		} else if ("流水号".equals(fieldName)) {
			bean.setNewAwayid(ve.getValue());
		} else if ("预支出差费用金额".equals(fieldName)) {
			bean.setAheadfee(ve.getValue());
		} else if ("出差类别".equals(fieldName)) {
			bean.setAwaytype(OA2HRConstant.AWAYTYPEARRAY[Integer.valueOf(ve
					.getValue())]);
		} else if ("原出差申请单".equals(fieldName)) {
			if (MyUtil.strIsNotNull(ve.getValue()))
				bean.setFormReference(ve.getValue());
		} else if ("出差时间起".equals(fieldName)) {
			awaybegindayList.add(MyUtil.getDate(ve.getValue()));
		} else if ("出差时间讫".equals(fieldName)) {
			awayenddayList.add(MyUtil.getDate(ve.getValue()));
		}
	}

	public String getFormName() {
		return "出差时间变更申请表";
	}

	public boolean save() {
		try {
			boolean success = false;
			bean.setAwaybeginday(MyUtil.getEarlyDay(awaybegindayList));
			bean.setAwayendday(MyUtil.getLastDay(awayenddayList));

			this.bean.accountAwayDays();

			AwayTimeChangeFormDao atcd = new AwayTimeChangeFormDao();

			atcd.setChuChaiBianGengBean(bean);

			if (atcd.findInitialFormNumber(!bean.getFormReference().matches(
					"^[0-9]*$"))) {
				log.info("-----save update");
				success = atcd.update();
			}
			atcd = null;
			return success;
		} finally {
			if (awaybegindayList != null)
				awaybegindayList.clear();
			awaybegindayList = null;
			if (awayenddayList != null)
				awayenddayList.clear();
			awayenddayList = null;

			bean = null;
		}
	}
}
